package demo4;

import java.util.Arrays;
import java.util.Comparator;

import static java.util.Arrays.*;

/**
 * Created with IntelliJ IEDA.
 * Description:
 * User:86186
 * Date:2022-05-20
 * Time:22:29
 */
class Student /*implements Comparable<Student>*/{
    public String name;
    public int age;
    public double score;

    public Student(String name, int age, double score) {
        this.name = name;
        this.age = age;
        this.score = score;
    }

    @Override
    public String toString() {
        return "Student{" +
                "name='" + name + '\'' +
                ", age=" + age +
                ", score=" + score +
                '}';
    }

    /*@Override
    public int compareTo(Student o) {
        return this.age-o.age;
    }*/
}
//根据年纪比较
class AgeComparator implements Comparator<Student> {

    @Override
    public int compare(Student o1, Student o2) {
        return o1.age-o2.age;
    }
}
//根据分数比较
class ScoreComparator implements  Comparator<Student>{

    @Override
    public int compare(Student o1, Student o2) {
        return (int)(o1.score-o2.score);
    }
}

public class Test {
    public static void main(String[] args) {
        Student[] stu = new Student[3];
        stu[0] = new Student("xiaowang",18,88.0);
        stu[1] = new Student("zhangsan",19,66.0);
        stu[2] = new Student("lisi",12,88.9);
        System.out.println("排序前" +Arrays.toString( stu));
        AgeComparator ageComparator = new AgeComparator();
        ScoreComparator scoreComparator = new ScoreComparator();
        Arrays.sort(stu,ageComparator);//要什么排序就给它传一个什么比较器就行了
        //比较两个对象的大小
        //error: if (student>student2){}
        System.out.println("排序后" +Arrays.toString( stu));
        System.out.println("-----------------------------------------------");
        System.out.println("排序前" +Arrays.toString( stu));
        Arrays.sort(stu,scoreComparator);//要什么排序就给它传一个什么比较器就行了
        //比较两个对象的大小
        //error: if (student>student2){}
        System.out.println("排序后" +Arrays.toString( stu));
    }
    public static void main4(String[] args) {
        Student student1 = new Student("bit",10,78.9);
        Student student2 = new Student("abc",11,88.9);
        //比较年龄
        AgeComparator ageComparator = new AgeComparator();
        int ret = ageComparator.compare(student1,student2);
        System.out.println(ret);
        //比较分数
        ScoreComparator scoreComparator = new ScoreComparator();
        int ret2 = scoreComparator.compare(student1,student2);
        System.out.println(ret2);

    }
    public static void main1(String[] args) {
        Student[] stu = new Student[3];
        stu[0] = new Student("xiaowang",18,88.0);
        stu[1] = new Student("zhangsan",19,66.0);
        stu[2] = new Student("lisi",12,88.9);
        System.out.println("排序前" +Arrays.toString( stu));
        Arrays.sort(stu);
        //比较两个对象的大小
        //error: if (student>student2){}
        System.out.println("排序后" +Arrays.toString( stu));
    }
}
